<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input v-model="queryInfo.query" placeholder="请输入订单编号" @clear="getOrdersList" clearable>
            <el-button slot="append" class="el-icon-search" @click="getOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="ordersList" style="width: 100%" @expand-change='getOrdersDetail' border="" stripe="">
        <el-table-column type="expand" label="明细" width="width" >
          <template slot-scope="scope">
            <!-- 订单下的商品列表 -->
            <el-table :data="scope.row.detail" style="width: 100%">
              <el-table-column prop="prop" label="封面" width="100px" align="center" header-align="center"> 
                  <template slot-scope="sc">
                  <!-- 实现图片点击放大效果 -->
                  <el-popover placement="top-start" trigger="hover">
                    <img :src="sc.row.url" style="width: 150px; height: 150px" />
                    <img :src="sc.row.url" slot="reference" style="width: 50px; height: 50px" />
                  </el-popover>
                </template>
              </el-table-column>
              <el-table-column prop="goods_name" label="商品名称" width="width"> </el-table-column>
              <el-table-column prop="goods_weight" label="商品重量" width="100px"> </el-table-column>
              <el-table-column prop="goods_number" label="商品数量" width="100px"> </el-table-column>
              <el-table-column prop="goods_price" label="商品价格(元)" width="100px"> </el-table-column>
              <el-table-column prop="goods_total_price" label="总价(元)" width="100px"> </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="width"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="width"> </el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)" width="width"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="width">
          <template slot-scope="scope">
            <el-tag type="success" size="mini" effect="dark" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" size="mini" effect="dark" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="width"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="width"> </el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改地址" placement="top" :enterable="false">
              <el-button type="primary" class="el-icon-edit" size="mini" @click="showEditDialog(scope.row.order_id)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="查看物流信息" placement="top" :enterable="false">
              <el-button type="warning" class="el-icon-setting" size="mini" @click="showProgressDialog(scope.row.order_id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
    <!-- 修改对话框 -->
    <el-dialog title="修改收货地址" :visible.sync="editDialogVisible" width="width" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" label-width="100px" ref="editFormRef">
        <el-form-item label="省市县/区" prop="address1">
          <!-- <el-input v-model="editForm"></el-input> -->
          <el-cascader :options="cityData" v-model="editForm.address1" :props="{ expandTrigger: 'hover' }"> </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddress">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查看物流信息对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.timestamp">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from "./citydata";
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagesize: 10,
        pagenum: 1,
      },
      ordersList: [],
      total: 0,
      editForm: {
        address1: [],
        address2: "",
      },
      editFormRules: {
        address1: [{ required: true, message: "请输入省市区/县", trigger: "blur" }],
        address2: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
      },
      cascaderProps: {
        label: "",
        value: "",
        children: "children",
        expandTrigger: "hover",
      },
      selectedKeys: [],
      editDialogVisible: false,
      cityData,
      //物流信息对话框隐藏
      progressDialogVisible: false,
      progressInfo: [],

      //详细情况
      detail:[]
    };
  },
  created() {
    this.getOrdersList();
  },
  computed: {},
  methods: {
    //获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取订单列表失败");
      }
      //提示获取订单列表成功
      this.$message.success("获取订单列表成功");
      this.ordersList = res.data.goods;
      this.total = res.data.total;
      //   console.log(res.data);
      //   console.log(this.ordersList);
      //时间格式
      this.ordersList.forEach((item) => {
        item.create_time = this.$tools.formatToTime(item.create_time);
        console.log(item)

        item.detail=[]

        console.log(item.detail)
      });
    },
    //分页
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize;
      this.getOrdersList();
    },
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum;
      this.getOrdersList();
    },
    //获取订单详情
    async getOrdersDetail(row) {
        // console.log(row)
      const { data: res } = await this.$http.get(`orders/${row.order_id}`);
      if (res.meta.status !== 200) {
        return this.$message.error("获取订单明细失败");
      }
      this.$message.success("获取订单明细成功");
      row.detail=res.data.goods
    //   this.detail=res.data.goods

      row.detail.forEach(async item=>{
          const{data:res}=await this.$http.get(`goods/${item.goods_id}`)
          if(res.meta.status!==200){
              return this.$message.error('获取商品详情失败')
          }
          this.$set(item,'goods_name',res.data.goods_name)
          this.$set(item,'goods_weight',res.data.goods_weight)
          this.$set(item,'url',res.data.pics[0].pics_big_url)
      })
      console.log(row)
      console.log(this.detail)
      console.log(res.data);
      console.log(res.data.goods);
    },
    //显示修改对话框
    showEditDialog() {
      this.editDialogVisible = true;
    },
    //修改地址
    async editAddress(id) {
      const { data: res } = await this.$http.put(`orders/${id}/address`, { consignee_addr: this.editForm.address1.join(",") + "," + this.editForm.address2 });
    //   console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("修改收货地址失败");
      }
      this.$message.success("修改收货地址成功");
      this.editDialogVisible = false;
    },
    //监听对话框关闭
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //显示查看物流信息对话框
    async showProgressDialog() {
      const { data: res } = await this.$http.get("/kuaidi/815294206237577");
      if (res.meta.status !== 200) {
        return this.$message.error("获取物流进度失败");
      }
      console.log(res.data);
      this.progressInfo = res.data;
      this.progressDialogVisible = true;
    },
    //查看物流信息
    checkProgress() {},
  },
};
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
